Imports System.Data
Imports System.Data.SqlClient

Partial Class GetPatients
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cnnPatientStr As SqlConnection
        Dim cmdPatientStr As SqlCommand
        Dim rdPatientStr As SqlDataReader
        Try
            Try
                If Me.Request.Params("name") <> Nothing Then
                    If Not ((Session("uid") Is Nothing) Or (Session("pwd") Is Nothing) Or (Session("uid") = "") Or (Session("pwd") = "")) Then
                        Dim strConnectionString As String
                        Dim strResponse As String
                        Dim strControl As String
                        strControl = Me.Request.Params("name")

                        Me.Response.Clear()
                        strConnectionString = ConfigurationManager.ConnectionStrings("DatabaseConnection").ConnectionString & "uid=" & Session("uid").ToString & ";pwd=" & Session("pwd").ToString
                        cnnPatientStr = New SqlConnection
                        cnnPatientStr.ConnectionString = strConnectionString
                        cnnPatientStr.Open()
                        cmdPatientStr = New SqlCommand
                        cmdPatientStr.Connection = cnnPatientStr
                        cmdPatientStr.CommandType = CommandType.Text
                        If Me.Request.Params(strControl) <> Nothing Then
                            If Me.Request.Params("name").IndexOf("tboxLastName") >= 0 Then
                                Me.Response.Write("<select name='ddlPatientList' id='ddlPatientList' style='width:470px;'>")
                                cmdPatientStr.CommandText = "SELECT PATIENT_ID,PATIENT_STRING FROM VW_PATIENT_STRING WHERE LAST_NAME LIKE '" + Me.Request.Params(strControl).ToString + "%'"
                                rdPatientStr = cmdPatientStr.ExecuteReader()
                                While rdPatientStr.Read
                                    strResponse = "<option value='" + rdPatientStr.GetInt32(0).ToString + "'>" + rdPatientStr.GetString(1) + "</option>"
                                    Me.Response.Write(strResponse)
                                End While
                                Me.Response.Write("</select>")
                            ElseIf Me.Request.Params("name").IndexOf("ddlPatientList") >= 0 Then
                                Dim strPrefix As String
                                If Me.Request.Params("prefix") <> Nothing Then
                                    strPrefix = Me.Request.Params("prefix")
                                Else
                                    strPrefix = ""
                                End If
                                cmdPatientStr.CommandText = "SELECT LAST_NAME,INITIALS,BIRTH_DATE,SEX FROM PATIENT WHERE PATIENT_ID=" + Me.Request.Params(strControl).ToString
                                rdPatientStr = cmdPatientStr.ExecuteReader()
                                rdPatientStr.Read()
                                strResponse = strPrefix + "tboxLastName;" + rdPatientStr.GetString(0).ToString + ";"
                                strResponse = strResponse + strPrefix + "tboxInitials;" + rdPatientStr.GetString(1).ToString + ";"
                                strResponse = strResponse + strPrefix + "tboxBirthDate;" + rdPatientStr.GetDateTime(2).ToString("dd\.MM\.yyyy") + ";"
                                strResponse = strResponse + strPrefix + "ddlSex;" + rdPatientStr.GetString(3).ToString
                                Me.Response.Write(strResponse)
                            End If
                        Else
                            Me.Response.Write("<select name='ddlPatientList' id='ddlPatientList' style='width:470px;'>")
                            Me.Response.Write("</select>")
                        End If
                        Me.Response.End()
                    End If
                End If
            Catch ex As SqlException
                Exit Sub
            End Try
        Finally
            If Not (rdPatientStr Is Nothing) Then
                rdPatientStr.Close()
                rdPatientStr = Nothing
            End If
            If Not (cnnPatientStr Is Nothing) Then
                cnnPatientStr.Close()
                cnnPatientStr = Nothing
            End If
            If Not (cmdPatientStr Is Nothing) Then
                cmdPatientStr.Dispose()
                cmdPatientStr = Nothing
            End If
        End Try
    End Sub
End Class
